package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.dto.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author alan
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
public class CourseController {

    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private EduSubjectService eduSubjectService;


    @ApiOperation("获取课程的详细信息")
    @PostMapping("/getCourseInfo/{id}")
    public R getCourseInfo(@PathVariable("id") String id) {
        Map<String, Object> result = eduCourseService.getCourseInfo(id);
        return R.ok().data(result);
    }

    @ApiOperation("分页获取前台的课程列表")
    @PostMapping("/pageCourse")
    public R pageCourse(@RequestBody CourseQuery query) {

        Page<EduCourse> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryChainWrapper<EduCourse> wrapper = eduCourseService.lambdaQuery()
                .select(EduCourse::getId, EduCourse::getTitle, EduCourse::getCover, EduCourse::getPrice, EduCourse::getBuyCount)
                .eq(StringUtils.isNotEmpty(query.getSubjectId()), EduCourse::getSubjectId, query.getSubjectId())
                .eq(StringUtils.isNotEmpty(query.getSubjectPId()), EduCourse::getSubjectParentId, query.getSubjectPId())
                .eq(EduCourse::getStatus, "Normal")
                .like(StringUtils.isNotEmpty(query.getKeyWord()), EduCourse::getTitle, query.getKeyWord());

        switch (query.getOrder()) {
            case "price":
                if (query.getOrderByAsc()) {
                    wrapper.orderByAsc(EduCourse::getPrice);
                } else {
                    wrapper.orderByDesc(EduCourse::getPrice);
                }
                break;
            case "id":
                if (query.getOrderByAsc()) {
                    wrapper.orderByAsc(EduCourse::getId);
                } else {
                    wrapper.orderByDesc(EduCourse::getId);
                }
                break;
            default:
                if (query.getOrderByAsc()) {
                    wrapper.orderByAsc(EduCourse::getViewCount);
                } else {
                    wrapper.orderByDesc(EduCourse::getViewCount);
                }
        }
        wrapper.page(page);

        return R.ok().data("courses", page.getRecords()).data("total", page.getTotal());

    }


    @ApiOperation("/获取课程分类一级分类")
    @PostMapping("/getFirstSubject")
    public R getFirstSubject() {

        List<EduSubject> list = eduSubjectService.lambdaQuery().eq(EduSubject::getParentId, 0)
                .select(EduSubject::getId, EduSubject::getTitle).list();
        return R.ok().data("fristSubject", list);

    }

    @ApiOperation("/获取课程分类二级分类")
    @PostMapping("/getSecondSubject/{pid}")
    public R getSecondSubject(@PathVariable("pid") String pid) {

        List<EduSubject> list = eduSubjectService.lambdaQuery().eq(EduSubject::getParentId, pid).select(EduSubject::getId,
                EduSubject::getTitle).list();
        return R.ok().data("secondSubject", list);

    }


}
